﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.DiemThuongKhachHang
{
    public partial class frmXuLy : Form
    {
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        #endregion

        #region khởi tạo biến
        bool kiemtra = false;
        bool checkClose = false;
        Entities.TyLeTinh temp;
        Entities.TyLeTinh tyletinh;
        public frmXuLy()
        {
            InitializeComponent();
            txtMaTyLeTinh.Text = getID();
            txtTyLeTinh.Focus();
        }
        #endregion

        #region ghi lại
        void GhiLai()
        {
            tyletinh = XuLyObject(Common.Constants.Insert);
            // check object
            if (tyletinh == null)
            {
                return;
            }
            // check conflic insert
            if (!CheckConflicInsert(tyletinh))
            {
                MessageBox.Show(Common.MSG.MSG5(tyletinh.MaTyLeTinh), Common.MSG.TieuDe, MessageBoxButtons.OK);
                txtMaTyLeTinh.Text = matyletinh;
                return;
            }
            // check insert
            tyletinh.TaiKhoan = UtilitiesVariable.User;
            if (Common.StaticVariable.InterfaceObject.InsertUpdateTyLeTinh(tyletinh) == 0)
            {
                return;
            }
            checkClose = true;
            this.Close();
        }
        #endregion

        #region Check Conflic Insert
        string matyletinh;
        public bool CheckConflicInsert(Entities.TyLeTinh tyletinh)
        {
            matyletinh = getID();
            if (tyletinh.MaTyLeTinh == matyletinh)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region thoát
        void Thoat()
        {
            this.Close();
        }
        #endregion

        #region xử lý id
        public string getID()
        {
            string str = "";
            Entities.GetID layid = new Entities.GetID();
            layid.TableName = "TyLeTinh";
            Entities.GetID lay = Common.StaticVariable.InterfaceObject.SelectID(layid);
            if (lay.ColumnName == null)
            {
                return "TLT_0001";
            }
            str = Common.Utilities.ProcessID(lay.ColumnName);
            return str;
        }
        #endregion

        #region validate
        public bool Validate()
        {
            if (txtTyLeTinh.Text == "")
            {
                MessageBox.Show(Common.MSG.MSG13(lbTyLeTinh.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                txtTyLeTinh.Focus();
                return false;
            }
            return true;
        }
        #endregion

        #region xử lý object

        public Entities.TyLeTinh XuLyObject(string hanhDong)
        {
            try
            {
                if (Validate())
                {
                    tyletinh = new Entities.TyLeTinh();
                    tyletinh.TyLeTinhID = 0;
                    tyletinh.MaTyLeTinh = txtMaTyLeTinh.Text;
                    tyletinh.Deleted = false;
                    tyletinh.GhiChu = txtGhiChu.Text;
                    tyletinh.HanhDong = hanhDong;
                    tyletinh.SoTien = txtTyLeTinh.Text;
                    tyletinh.NgayNhap = Common.StaticVariable.InterfaceObject.SelectDate().Date;
                    return tyletinh;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region Event
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            btnGhiLai.Click += new EventHandler(btnGhiLai_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            this.FormClosing += new FormClosingEventHandler(frmXuLy_FormClosing);
            txtTyLeTinh.TextChanged += new EventHandler(txtTyLeTinh_TextChanged);
        }

        void txtTyLeTinh_TextChanged(object sender, EventArgs e)
        {
            TextBox temp = (TextBox)sender;
            int vt = temp.SelectionStart;
            string str = new string(temp.Text.ToList<char>().Where(c => char.IsNumber(c)).ToArray<char>());

            temp.Text = str;

            temp.SelectionStart = vt;
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void btnGhiLai_Click(object sender, EventArgs e)
        {
            GhiLai();
        }

        void frmXuLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
            else
            {
                GetData(kiemtra);
            }
        }
        #endregion
    }
}
#endregion